package com.tim.helper.analysis;

import com.tim.third.MavenPomUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Comparator;

/**
 * 入口分析：
 *  前端；分析controller
 *  回调；回调中间页、回调接口
 *  mq；rabbitmq、rocketmq，对应查找监听器
 *      rocketmq层级：group、topic、message-tag
 *      rabbitmq层级：exchange、topic、message
 *  事件；ApplicationEvent。检索监听器 @EventListener
 *  定时任务；@Scheduled、quartz、xxl-job、elastic-job
 * 日志：
 *  各日志文件记录内容
 *  日志检索风格；关键词是url、方法签名？参数
 * 检索方式：检索类引用、字段引用、方法调用等
 *
 * https://c.runoob.com/front-end/3602/ ；unicode转gbk2312
 */
public class ProjectAnalysis {
    SrewPluginHelper h1;// 数据库表分析
    SwaggerAnalysis h2;// controller入口分析

    @Test// 检索消息监听器
    public void searchMqListener(){
        /*
         * rocketmq
         *  通用注册 @RocketMQMessageListener
         *  项目特殊注册 RocketMqMessgeSuscriber
         *
         * rabbitmq
         *  通用注册 @RabbitListener
         *  项目特殊注册
         *
         *
         */



    }

    @Test// 检索事件处理器
    public void searchEventListener(){
        /*
         *  通用注册 @EventListener ApplicationListener
         *  项目特殊注册
         *
         */

    }

    @Test// 检索定时任务 SimpleJob
    public void searchScheduleTask(){
        /*
         *  @Scheduled 定时任务
         *
         *  quartz 检索：
         *
         *  xxl-job 检索：
         *
         *  elastic-job
         *      检索：SimpleJob
         *      项目特殊检索：AbstractJob
         *
         *  项目特殊注册 CronService
         *
         */

    }

    @Test// 各日志文件记录内容、日志检索风格
    public void logInfo(){
        //

    }


    @Test
    public void printAutilProjectPom() throws Exception{
        // 项目根路径，检索pom，解析module输出依赖
        String path = "D:\\github\\platform";

        int hasPom = 1;// 0无，1有
        path = path.trim();
        path = path.endsWith("\\")||path.endsWith("/") ? path : path+"/";
        String pomPath = path + "pom.xml";
        File pomFile = new File(pomPath);
        if(!pomFile.exists()){
            hasPom = 0;
        }

        StringBuffer buffer = new StringBuffer();
        if(hasPom==0){
            // 若无pom，检索子文件夹
            MavenPomUtils.getDirModule(path, buffer);
        }else{
            MavenPomUtils.getProjectModule(path, buffer);
        }
        System.out.println(buffer);
    }

    @Test
    public void genBuildParentPom(){
        String dir = "D:\\github\\platform";

        String[] split = dir.trim().split("[/\\\\]+");
        String projectName = split[split.length-1];
        StringBuilder builder = new StringBuilder();
        Arrays.stream(new File(dir).listFiles(File::isDirectory))
                .map(File::getName)
                .filter(item->!item.startsWith("."))
                .forEach(item->builder.append(String.format("        <module>%s</module>\n", item)));
        String template = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
                "\n" +
                "    <groupId>com.mvn</groupId>\n" +
                "    <artifactId>build-use-%s</artifactId>\n" +
                "    <version>1.0.0-SNAPSHOT</version>\n" +
                "    <name>build-use</name>\n" +
                "    <description>build-use</description>\n" +
                "    <modelVersion>4.0.0</modelVersion>\n" +
                "    <packaging>pom</packaging>\n" +
                "    <modules>\n%s" +
                "    </modules>\n" +
                "\n" +
                "    <repositories>\n" +
                "        <repository>\n" +
                "            <id>tencent-central</id>\n" +
                "            <name>tencent mirror central</name>\n" +
                "            <url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>\n" +
                "            <snapshots>\n" +
                "                <enabled>true</enabled>\n" +
                "            </snapshots>\n" +
                "            <releases>\n" +
                "                <enabled>true</enabled>\n" +
                "            </releases>\n" +
                "        </repository>\n" +
                "    </repositories>\n" +
                "\n" +
                "    <pluginRepositories>\n" +
                "        <pluginRepository>\n" +
                "            <id>tencent-plugin-central</id>\n" +
                "            <url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>\n" +
                "            <snapshots>\n" +
                "                <enabled>true</enabled>\n" +
                "            </snapshots>\n" +
                "            <releases>\n" +
                "                <enabled>true</enabled>\n" +
                "            </releases>\n" +
                "        </pluginRepository>\n" +
                "    </pluginRepositories>\n" +
                "</project>\n";
        String content = String.format(template, projectName, builder);
        System.out.println(content);
    }

    @Test// 读取配置文件，输出包含http、https的行。分析第三方应用
    public void printHttpLine() throws IOException {
        String path = "E:\\gitlab\\xqxc\\settings\\dev\\xqxc-dev.properties";

        String[] searchs = new String[]{"HTTP:", "HTTPS:"};
        Files.readAllLines(Paths.get(path)).stream()
                .filter(StringUtils::isNotBlank)
                .filter(e->!e.trim().startsWith("#"))
                .filter(e->StringUtils.containsAny(e.toUpperCase(), searchs))
                .sorted(Comparator.naturalOrder())
                .forEach(System.out::println);
    }



    /**项目总览记录*/
    static class Pandect {
        class Mq {
            /* step1，注册发布、监听*/
//            RocketMqMessageSender sender;// 注册发布
//            RewriteRocketMqMessgeSuscriber suscriber;// 注册监听

            /* step2，sender*/
            public void sender(){
                // report
                {
                    // 报表模块消息发送器
                    // mq.report.export.status.mqTopic REPORT_EXPORT_STATUS_TOPIC 报表状态更新topic
//                    ReportMessageSender s1;

                    // mq.microep.reconciliation.report.topic 导出微企对账单
//                    MicroEpReconReportExecuter s8;
                }

            }

            /* step3，group、topic、处理器*/
            public void listener(){
                // biz-pig-impl项目groupId：
                // GID_BIZPIG
                // GID_RIDER_ASSIGN
                // GID_BIZPIG_SETTLE_NOTIFY
                {
                    // topic枚举；项目中的topic写到一个枚举

                    // groupId：GID_BIZPIG_SETTLE_NOTIFY
                    // mq.pay.handle.settle.order.notify.topic	SETTLE_ORDER_NOTIFY_TOPIC
//                    com.xqxc.api.impl.pig.biz.mq.listener.PayListener l1;//

                }

            }


        }

        class Event {
// 支付错误日志事件
//        PayErrLogEvent e1;
//        PayErrLogEvetListener h1;
        }

        class ScheduledTask {
            /* step1，@Scheduled 定时任务*/
            {
//            GZUnionWalletServiceImpl d1;// 银联钱包交易记录，获取前一天的数据落库
            }

            /* step2，框架定时任务*/
            // 项目定时任务框架：elastic-job
            {
                // 配置、注册。xml注册、api注册

                // 定时任务
//            ConfigJob d1;// api注册，首次执行触发注册job；后续监视job配置表，刷新job

            }

        }

    }


}

